home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pavt150.zip / CAPI.ZIP / JAMSTORE.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  6KB  |  211 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Joaquim Homrighausen.
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamstore.c (JAMmb)
  11. **
  12. **  Store messages and texts
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    JoHo
  18. **  Initial coding. No encryption or escaping supported yet.
  19. */
  20. #define JAMCAPI 1
  21.  
  22. #include <string.h>
  23. #include "jammb.h"
  24.  
  25. /*
  26. **  Store message header with specified number. Returns 1 upon success
  27. **  and 0 upon failure. The HdrHandle's file offset will point to the
  28. **  end of the fixed-length header when the function returns (if
  29. **  successful) so the application can write any subfields directly to
  30. **  the file.
  31. */
  32. int _JAMPROC JAMmbStoreMsgHdr(JAMAPIRECptr apirec, UINT32 WhatMsg)
  33. {
  34.     /* Fetch index record, checks for IsOpen and valid msg number */
  35.     if (!JAMmbFetchMsgIdx(apirec, WhatMsg))
  36.         return (0);
  37.  
  38.     /* Make sure it's locked */
  39.     if (!apirec->HaveLock)
  40.         {
  41.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  42.         return (0);
  43.         }
  44.  
  45.     /* Update structure, even if below fails */
  46.     apirec->Hdr.MsgNum=WhatMsg;
  47.  
  48.     /* Make sure subfield len is has the correct length, if the platform */
  49.     /* requires alignment */
  50.     apirec->Hdr.SubfieldLen=JAMsysAlign(apirec->Hdr.SubfieldLen);
  51.  
  52.     /* Make sure header signature and revision is OK */
  53.     strcpy(apirec->Hdr.Signature, HEADERSIGNATURE);
  54.     apirec->Hdr.Revision=CURRENTREVLEV;
  55.  
  56.     /* Write header */
  57.     if (apirec->SeekFunc(apirec, apirec->HdrHandle, JAMSEEK_SET, (INT32)apirec->Idx.HdrOffset)!=(INT32)apirec->Idx.HdrOffset)
  58.         {
  59.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  60.         return (0);
  61.         }
  62.     if (apirec->WriteFunc(apirec, apirec->HdrHandle, &apirec->Hdr, (INT32)sizeof(JAMHDR))!=(INT32)sizeof(JAMHDR))
  63.         {
  64.         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  65.         return (0);
  66.         }
  67.  
  68.     /* Wrote it OK */
  69.     apirec->APImsg=JAMAPIMSG_NOTHING;
  70.     return (1);
  71. }
  72.  
  73. /*
  74. **  Store message index record with specified number. Returns 1 upon
  75. **  success and 0 upon failure. The IdxHandle's file offset will point
  76. **  to the end of the fixed-length index record when the function
  77. **  returns (if successful).
  78. */
  79. int _JAMPROC JAMmbStoreMsgIdx(JAMAPIRECptr apirec, UINT32 WhatMsg)
  80. {
  81.     INT32 WhatOffset;
  82.  
  83.     /* Make sure it's open */
  84.     if (!apirec->isOpen)
  85.         {
  86.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  87.         return (0);
  88.         }
  89.  
  90.     /* Make sure it's locked */
  91.     if (!apirec->HaveLock)
  92.         {
  93.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  94.         return (0);
  95.         }
  96.  
  97.     /* Make sure the message number is valid */
  98.     if (WhatMsg<apirec->HdrInfo.BaseMsgNum)
  99.         {
  100.         apirec->APImsg=JAMAPIMSG_INVMSGNUM;
  101.         return (0);
  102.         }
  103.  
  104.     /* Store index record */
  105.     WhatOffset=(INT32)((WhatMsg-apirec->HdrInfo.BaseMsgNum) * (INT32)sizeof(JAMIDXREC));
  106.     if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, WhatOffset)!=WhatOffset)
  107.         {
  108.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  109.         return (0);
  110.         }
  111.     if (apirec->WriteFunc(apirec, apirec->IdxHandle, &apirec->Idx, (INT32)sizeof(JAMIDXREC))!=(INT32)sizeof(JAMIDXREC))
  112.         {
  113.         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  114.         return (0);
  115.         }
  116.  
  117.     /* Wrote it OK */
  118.     apirec->APImsg=JAMAPIMSG_NOTHING;
  119.     return (1);
  120. }
  121.  
  122. /*
  123. **  Store message text at current header's text position. Returns 1 upon
  124. **  success and 0 upon failure. The TxtHandle's file offset will point to
  125. **  the end of the written text block when the function returns (if
  126. **  successful). This assumes that the entire message text is stored in
  127. **  the internal buffer. See JAMMBSTOREMSGTXTBUF() for a function that
  128. **  takes an external parameter and length specifier.
  129. */
  130. int _JAMPROC JAMmbStoreMsgTxt(JAMAPIRECptr apirec)
  131. {
  132.     /* Make sure it's open */
  133.     if (!apirec->isOpen)
  134.         {
  135.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  136.         return (0);
  137.         }
  138.  
  139.     /* Make sure it's locked */
  140.     if (!apirec->HaveLock)
  141.         {
  142.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  143.         return (0);
  144.         }
  145.  
  146.     /* Write text */
  147.     if (apirec->SeekFunc(apirec, apirec->TxtHandle, JAMSEEK_SET, (INT32)apirec->Hdr.TxtOffset)!=(INT32)apirec->Hdr.TxtOffset)
  148.         {
  149.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  150.         return (0);
  151.         }
  152.     if (apirec->WriteFunc(apirec, apirec->TxtHandle, apirec->WorkBuf, (INT32)apirec->Hdr.TxtLen)!=(INT32)apirec->Hdr.TxtLen)
  153.         {
  154.         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  155.         return (0);
  156.         }
  157.  
  158.     /* Wrote it OK */
  159.     apirec->APImsg=JAMAPIMSG_NOTHING;
  160.     return (1);
  161. }
  162.  
  163. /*
  164. **  Store message text at current header's text position. Returns 1 upon
  165. **  success and 0 upon failure. The TxtHandle's file offset will point to
  166. **  the end of the written text block when the function returns (if
  167. **  successful). The ISFIRST parameter determines if the function should
  168. **  seek to the position specified in the header before writing. This
  169. **  allows multiple calls to write large message texts.
  170. */
  171. int _JAMPROC JAMmbStoreMsgTxtBuf(JAMAPIRECptr apirec, CHAR8ptr Buffer,
  172.                                   INT32 BufLen, int IsFirst)
  173. {
  174.     /* Make sure it's open */
  175.     if (!apirec->isOpen)
  176.         {
  177.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  178.         return (0);
  179.         }
  180.  
  181.     /* Make sure it's locked */
  182.     if (!apirec->HaveLock)
  183.         {
  184.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  185.         return (0);
  186.         }
  187.  
  188.     /* Seek if we're told to */
  189.     if (IsFirst)
  190.         {
  191.         if (apirec->SeekFunc(apirec, apirec->TxtHandle, JAMSEEK_SET, (INT32)apirec->Hdr.TxtOffset)!=(INT32)apirec->Hdr.TxtOffset)
  192.             {
  193.             apirec->APImsg=JAMAPIMSG_SEEKERROR;
  194.             return (0);
  195.             }
  196.         }
  197.  
  198.     /* Write the text */
  199.     if (apirec->WriteFunc(apirec, apirec->TxtHandle, Buffer, BufLen)!=BufLen)
  200.         {
  201.         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  202.         return (0);
  203.         }
  204.  
  205.     /* Wrote it OK */
  206.     apirec->APImsg=JAMAPIMSG_NOTHING;
  207.     return (1);
  208. }
  209.  
  210. /* end of file "jamstore.c" */
  211.